package com.cartoon.service.impl;

import com.cartoon.entity.Cartoon;
import com.cartoon.entity.Chapter;
import com.cartoon.dao.ChapterDao;
import com.cartoon.service.ChapterService;
import com.cartoon.util.ResponseData;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (Chapter)表服务实现类
 *
 * @author makejava
 * @since 2021-12-10 11:28:08
 */
@Service("chapterService")
public class ChapterServiceImpl implements ChapterService {
    @Resource
    private ChapterDao chapterDao;


    @Override
    public ResponseData getChapterByCartoonId(int cartoonId) {
        try{
            List<Chapter> chapterList = new ArrayList<>();
            List<Chapter> list = chapterDao.getChapterByCartoonId(cartoonId);
            if(list.size()!=0){
                chapterList.add(list.get(0));
                for(int chapterx=0;chapterx<list.size();chapterx++){
                    for(int num=0;num<chapterList.size();num++){
                      if(chapterList.get(num).getChapternumber()==list.get(chapterx).getChapternumber()){
                          break;
                      }
                      else if(num==chapterList.size()-1){
                          chapterList.add(list.get(chapterx));
                      }
                    }
                }
            }
            return new ResponseData("0","请求成功",chapterList);
        }
        catch (Exception e){
            e.printStackTrace();
            return new ResponseData("9924","请求失败了");
        }
    }

    @Override
    public ResponseData getContentByCartoonId(Integer cartoonId, Integer chapterNumber) {
       try{
           return new ResponseData("0","查询成功",chapterDao.getContentByCartoonId(cartoonId,chapterNumber));
       }
       catch (Exception e){
           e.printStackTrace();
           return new ResponseData("9924","出错啦");
       }
    }

    @Override
    public ResponseData getContentByChapterNumber(int cartoonId,int chapterNumber) {
        try{
            return new ResponseData("0","请求成功",chapterDao.getContentByChapterNumber(cartoonId,chapterNumber)) ;
        }
        catch (Exception e){
            e.printStackTrace();
            return new ResponseData("9924","出错了哥");
        }
    }
}
